using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using Emlyn.Website.Users;
using System.ComponentModel;

namespace Emlyn.Website.UsersPresentation
{
    public class PresentUserList: CompositeControl 
    {
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string UrlPrefix
        {
            get { return Convert.ToString(ViewState["UrlPrefix"]); }
            set { ViewState["UrlPrefix"] = value; }
        }


        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public IList<User> UserList
        {
            get
            {
                IList<User> retval = new List<User>();

                string luserGuids = Convert.ToString(ViewState["UserGuids"]);
                if (!String.IsNullOrEmpty(luserGuids))
                {
                    string[] lUserGuidArr = luserGuids.Split(',');
                    UserQuery luq = new UserQuery();

                    foreach (string luserGuid in lUserGuidArr)
                    {
                        User luser = luq.Select(new Guid(luserGuid));
                        retval.Add(luser);
                    }
                }

                return retval;
            }
            set
            {
                string luserGuids = String.Empty;

                foreach (User luser in value)
                {
                    if (!String.IsNullOrEmpty(luserGuids))
                        luserGuids += ",";
                    luserGuids += luser.UserID.ToString();
                }

                ViewState["UserGuids"] = luserGuids;
            }
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            Panel lbasePanel = new Panel();
            lbasePanel.CssClass = CssClass + "UserList";
            Controls.Add(lbasePanel);

            foreach (User luser in UserList)
            {
                Panel lpnlUser = new Panel();
                lpnlUser.CssClass = CssClass + "User";
                lbasePanel.Controls.Add(lpnlUser);

                Label lblUserName = new Label();
                lblUserName.Text = luser.UserName;
                lpnlUser.Controls.Add(lblUserName);

                if (!String.IsNullOrEmpty(UrlPrefix))
                {
                    HyperLink hlUserName = new HyperLink();
                    hlUserName.Controls.Add(lblUserName);
                    hlUserName.NavigateUrl = UrlPrefix.Replace("[[USERID]]", System.Web.HttpUtility.UrlEncode(luser.UserID.ToString()));
                    lpnlUser.Controls.Add(hlUserName);
                }
                else
                {
                    lpnlUser.Controls.Add(lblUserName);
                }
            }
        }
    }
}
